/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.ConnReader;
import Controller.Connection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author nexo__000
 */
public class EditorConex extends JFrame implements ActionListener, MouseListener {

    private Vista vista;
    private JLabel jlNombre;
    private JComboBox jcbNombre;
    private JLabel jlHost;
    private JTextField jtfHost;
    private JLabel jlUser;
    private JTextField jtfUser;
    private JLabel jlPass;
    private JTextField jtfPass;
    private JButton jbGuardar;
    private JButton jbCancelar;
    private JButton jbEliminar;
    private JButton jbAceptar;

    public EditorConex() {
        super();
        vista = Controller.Controller.getVista();
        initComponents();
        loadCombo();
    }

    private void initComponents() {
        try {
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGap(0, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGap(0, 300, Short.MAX_VALUE)
            );
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            pack();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(EditorConex.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(EditorConex.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(EditorConex.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(EditorConex.class.getName()).log(Level.SEVERE, null, ex);
        }
        setTitle("Editor de conexiones");
        getContentPane().setLayout(null);
        setSize(400, 300);
        setLocationRelativeTo(null);
        getContentPane().setLayout(null);

        //Añadimos los botones y los labels
        jlNombre = new JLabel("Nombre de la conexion:");
        jlNombre.setBounds(20, 20, 340, 24);
        add(jlNombre);
        jcbNombre = new JComboBox();
        jcbNombre.setBounds(20, 44, 340, 24);
        jcbNombre.setEditable(true);
        jcbNombre.addItemListener(new ItemChangeListener());
        add(jcbNombre);

        jlHost = new JLabel("Host de conexión:");
        jlHost.setBounds(20, 94, 100, 24);
        add(jlHost);
        jtfHost = new JTextField();
        jtfHost.setBounds(120, 94, 240, 24);
        add(jtfHost);

        jlUser = new JLabel("Usuario:");
        jlUser.setBounds(20, 128, 100, 24);
        add(jlUser);
        jtfUser = new JTextField();
        jtfUser.setBounds(120, 128, 240, 24);
        add(jtfUser);

        jlPass = new JLabel("Contraseña:");
        jlPass.setBounds(20, 162, 100, 24);
        add(jlPass);
        jtfPass = new JTextField();
        jtfPass.setBounds(120, 162, 240, 24);
        add(jtfPass);

        jbCancelar = new JButton("Cancelar");
        jbAceptar = new JButton("Aceptar");
        jbGuardar = new JButton("Guardar");
        jbEliminar = new JButton("Eliminar");
        jbEliminar.setBounds(20, 226, 80, 24);
        jbGuardar.setBounds(108, 226, 80, 24);
        jbCancelar.setBounds(194, 226, 80, 24);
        jbAceptar.setBounds(280, 226, 80, 24);
        jbCancelar.addMouseListener(this);
        add(jbCancelar);
        jbAceptar.addMouseListener(this);
        add(jbAceptar);
        jbGuardar.addMouseListener(this);
        add(jbGuardar);
        jbEliminar.addMouseListener(this);
        add(jbEliminar);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        if (SwingUtilities.isLeftMouseButton(me)) {
            if (me.getSource().equals(jbAceptar)) {
                vista.setConnection((String) jcbNombre.getSelectedItem());
                vista.loadComboConnections();
                vista.selectComboItem((String) jcbNombre.getSelectedItem());
                dispose();
            } else if (me.getSource().equals(jbCancelar)) {
                dispose();
                vista.loadComboConnections();
            } else if (me.getSource().equals(jbGuardar)) {
                ConnReader cr = new ConnReader();
                Connection con = new Connection(
                        (String) jcbNombre.getSelectedItem(),
                        jtfHost.getText(),
                        jtfUser.getText(),
                        jtfPass.getText());
                if (cr.find((String) jcbNombre.getSelectedItem()) == -1) {
                    cr.add(con);
                } else {
                    cr.modify(
                            cr.find((String) jcbNombre.getSelectedItem()),
                            (String) jcbNombre.getSelectedItem(),
                            jtfHost.getText(),
                            jtfUser.getText(),
                            jtfPass.getText());
                }
                loadCombo();
            } else if (me.getSource().equals(jbEliminar)) {
                ConnReader cr = new ConnReader();
                cr.delete(cr.find((String) jcbNombre.getSelectedItem()));
                loadCombo();
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent me) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseReleased(MouseEvent me
    ) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseEntered(MouseEvent me
    ) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void mouseExited(MouseEvent me
    ) {
//        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void loadCombo() {
        ConnReader cr = new ConnReader();
        jcbNombre.setModel(new DefaultComboBoxModel(cr.getNames()));
        setFields();
    }

    public void setFields() {
        ConnReader cr = new ConnReader();
        int index = cr.find((String) jcbNombre.getSelectedItem());
        if (index != -1) {
            Connection con = cr.get(index);
            jtfHost.setText(con.getHost());
            jtfUser.setText(con.getUser());
            jtfPass.setText(con.getPass());
        }
    }

    class ItemChangeListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent event) {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                setFields();
            }
        }
    }

}
